<template>
  <van-nav-bar left-arrow title="订单结算" fixed @click-left="onBack" />

  <div style="height: 46px"></div>

  <van-contact-card type="add" add-text="选择收货人" @click="onAdd" v-if="JSON.stringify(AddressInfo) === '{}'" />

  <van-contact-card type="edit" :tel="AddressInfo.mobile" :name="AddressInfo.consignee" v-else @click="onEdit" />

  <!-- 选择的购物车的商品数据 -->
  <van-card
    v-if="CartList.length != 0"
    v-for="item in CartList"
    :key="item.id"
    :num="item.pronum"
    :price="item.total"
    :title="item.product.name"
    :thumb="item.product.cover_cdn"
  >
    <template #desc>
      <span>商品单价：￥{{ item.price }}</span>
    </template>
  </van-card>

  <van-card v-else :price="info.price" :title="info.name" :num="num" :thumb="info.cover_cdn">
    <template #desc>
      <span>商品单价：￥{{ info.price }}</span>
    </template>
  </van-card>

  <!-- 订单备注 -->
  <van-cell-group inset>
    <van-field v-model="content" rows="2" autosize label="订单备注" type="textarea" maxlength="50" placeholder="请输入订单备注" show-word-limit />
  </van-cell-group>

  <van-submit-bar :price="total" button-text="结算" @submit="onSubmit" />
  <!-- <van-submit-bar v-if="CartList.length != 0" :price="total" button-text="结算" @submit="onSubmit" />
  <van-submit-bar v-else :price="Buytotal" button-text="结算" @submit="onSubmit" /> -->
</template>

<script>
  import { showNotify } from 'vant'
  export default {
    data() {
      return {
        AddressInfo: {},
        LoginAuth: {},
        CartList: [],
        content: '',
        productinfo: {},
        info: {},
        num: 0,
      }
    },
    created() {
      // 当前收货地址
      let address = this.$cookies.get('address') ? this.$cookies.get('address') : {}

      this.LoginAuth = this.$cookies.get('LoginAuth') ? this.$cookies.get('LoginAuth') : {}

      if (JSON.stringify(address) !== '{}') {
        this.AddressInfo = address
      } else {
        this.getAddressDefault()
      }

      this.getConfirmData()
      console.log(this.total)
    },
    methods: {
      onBack() {
        if (this.$route.query.action == 'Buy') {
          this.$router.go(-1)
        } else {
          this.$router.go(-1)
        }
      },
      onAdd() {},
      async getAddressDefault() {
        let data = {
          busid: this.LoginAuth.id,
        }

        let result = await this.$api.AddressDefault(data)

        if (result.code === 1) {
          this.AddressInfo = result.data
        }
      },
      // 更改收货地址
      onEdit() {
        this.$router.push({ path: '/business/address/index', query: { action: 'order' } })
      },

      // 确认订单信息
      async getConfirmData() {
        // CartConfirmData

        // 如果是从直接购买过来的订单
        if (this.$route.query.action == 'Buy') {
          let proid = this.$cookies.get('productinfo') ? this.$cookies.get('productinfo') : ''
          this.num = this.$cookies.get('productinfo').num ? this.$cookies.get('productinfo').num : ''
          // console.log(this.num)
          // console.log(proid)
          // console.log(this.CartList)
          // 调用接口查询信息
          let result = await this.$api.ProductInfoData(proid)

          if (result.code === 0) {
            showNotify({
              type: 'warning',
              message: result.msg,
              duration: 1500,
              onClose: () => {
                this.$router.go(-1)
              },
            })
            return false
          }

          this.info = result.data

          console.log(this.info)
        } else {
          let cartids = this.$route.query.cartids ? this.$route.query.cartids : ''

          let result = await this.$api.CartConfirmData({ cartids })

          if (result.code === 0) {
            showNotify({
              type: 'warning',
              message: result.msg,
              duration: 1500,
              onClose: () => {
                this.$router.go(-1)
              },
            })

            return false
          }

          this.CartList = result.data
        }
      },
      // 订单结算
      async onSubmit() {
        // 直接购买走这个逻辑
        if (this.$route.query.action == 'Buy') {
          // productinfo的cookies里面有两个数据，要单独点出来
          let proid = this.$cookies.get('productinfo').proid ? this.$cookies.get('productinfo').proid : ''
          let data = {
            proid: proid,
            busid: this.LoginAuth.id,
            content: this.content,
            addrid: this.AddressInfo.id,
            num: this.num,
            total: (this.total / 100).toFixed(2),
          }
          console.log(data)

          let result = await this.$api.BuySubmit(data)
          // 下单成功
          if (result.code === 1) {
            showNotify({
              type: 'success',
              message: result.msg,
              duration: 1500,
              onClose: () => {
                this.$cookies.remove('address')

                this.$router.push('/order/order/index')
              },
            })
          } else {
            showNotify({
              type: 'warning',
              message: result.msg,
              duration: 1500,
            })
          }
        } else {
          // 获取购物车的id
          let cartids = this.$route.query.cartids ? this.$route.query.cartids : ''

          // 提交订单时传的数据
          let data = {
            cartids: cartids,
            busid: this.LoginAuth.id,
            content: this.content,
            addrid: this.AddressInfo.id,
          }
          // console.log(data)

          let result = await this.$api.OrderSubmit(data)
          // 下单成功
          if (result.code === 1) {
            showNotify({
              type: 'success',
              message: result.msg,
              duration: 1500,
              onClose: () => {
                this.$cookies.remove('address')

                this.$router.push('/order/order/index')
              },
            })
          } else {
            showNotify({
              type: 'warning',
              message: result.msg,
              duration: 1500,
            })
          }
        }
      },
    },
    computed: {
      total() {
        let total = 0
        this.num = this.$cookies.get('productinfo').num ? this.$cookies.get('productinfo').num : ''

        if (this.$route.query.action == 'Buy') {
          total = this.num * this.info.price

          return total * 100
        } else {
          if (this.CartList.length > 0) {
            for (let item of this.CartList) {
              total += parseFloat(item.total)
            }
          }

          return total * 100
        }
      },
    },
  }
</script>
